<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Company;

/**
 * CompanySearch represents the model behind the search form about `common\models\Company`.
 */
class CompanySearch extends Company
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['company_id', 'company_market_if', 'company_hr_status', 'company_mdm_status'], 'integer'],
            [['company_name', 'company_plate', 'company_mdm_id', 'company_parent_mdm_id', 'company_hr_id', 'company_finance_id', 'company_import_id', 'company_abb', 'company_ct_start_date', 'company_ct_end_date', 'company_most_abb', 'company_update_reason', 'company_memo', 'company_created_by', 'company_created_at', 'company_updated_by', 'company_updated_at'], 'safe'],
            [['company_longitude', 'company_latitude'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Company::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'company_id' => $this->company_id,
            'company_market_if' => $this->company_market_if,
            'company_ct_start_date' => $this->company_ct_start_date,
            'company_ct_end_date' => $this->company_ct_end_date,
            'company_longitude' => $this->company_longitude,
            'company_latitude' => $this->company_latitude,
            'company_hr_status' => $this->company_hr_status,
            'company_mdm_status' => $this->company_mdm_status,
            'company_created_at' => $this->company_created_at,
            'company_updated_at' => $this->company_updated_at,
        ]);

        $query->andFilterWhere(['like', 'company_name', $this->company_name])
            ->andFilterWhere(['like', 'company_plate', $this->company_plate])
            ->andFilterWhere(['like', 'company_mdm_id', $this->company_mdm_id])
            ->andFilterWhere(['like', 'company_parent_mdm_id', $this->company_parent_mdm_id])
            ->andFilterWhere(['like', 'company_hr_id', $this->company_hr_id])
            ->andFilterWhere(['like', 'company_finance_id', $this->company_finance_id])
            ->andFilterWhere(['like', 'company_import_id', $this->company_import_id])
            ->andFilterWhere(['like', 'company_abb', $this->company_abb])
            ->andFilterWhere(['like', 'company_most_abb', $this->company_most_abb])
            ->andFilterWhere(['like', 'company_update_reason', $this->company_update_reason])
            ->andFilterWhere(['like', 'company_memo', $this->company_memo])
            ->andFilterWhere(['like', 'company_created_by', $this->company_created_by])
            ->andFilterWhere(['like', 'company_updated_by', $this->company_updated_by]);

        return $dataProvider;
    }
}
